package
{
	import flash.display.Sprite;
	import flash.geom.Point;

	public class ParallaxStarfield extends Sprite
	{
		private var layer1:Sprite;
		private var layer2:Sprite;
		private var layer3:Sprite;
		private var layer4:Sprite;
		
		public function ParallaxStarfield()
		{
			var density:int = 300;
			var size:int = 2000;
			var starsize:int = 1;
			layer1 = createStarfield(density,size*1,starsize);
			layer2 = createStarfield(density/2,size*1.3,starsize*2);
			layer3 = createStarfield(density/4,size*1.6,starsize*4);
			layer4 = createStarfield(density/8,size*2,starsize*8);
			
			addChild(layer1);
			//center(layer1);
			
			addChild(layer2);
			//center(layer2);
			
			addChild(layer3);
			//center(layer3);
			
			addChild(layer4);
			//center(layer4);
		}
		
		public function center(layer:Sprite):void{
			layer.x = -layer.width/2;
			layer.y = -layer.height/2;			
		}
		
		public function change(point:Point):void{

			layer1.x += point.x/16;
			layer1.y += point.y/16;
			
			layer2.x += point.x/8;
			layer2.y += point.y/8;

			layer3.x += point.x/4;
			layer3.y += point.y/4;
			
			layer4.x += point.x/2;
			layer4.y += point.y/2;			
			
			//layer3.x += point.x;
			//layer3.y += point.y;
			
//			layer3.x += point.x*4;
//			layer3.y += point.y*4;
//			
//			layer4.x += point.x*6;
//			layer4.y += point.y*6;			
		}
		
		
		
		
		public static function createStarfield(density:int = 100, size:int = 500, starSize:Number = 1):Sprite{
			var i:int;
			var starfield:Sprite = new Sprite();
			//starfield.graphics.beginFill(Math.floor(Math.random()*0x0000ff)*0x111111);
			starfield.graphics.beginFill(0xdddddd);
			for(i = 0; i < density; i++){
				var pnt:Point = new Point(Math.floor(Math.random()*size),Math.floor(Math.random()*size));
				//starfield.graphics.drawCircle(pnt.x,pnt.y,starSize);
				starfield.graphics.drawRect(pnt.x,pnt.y,starSize,starSize);
			}
			starfield.graphics.endFill();
			
			return starfield;			
		}
				
	}
}